package com.gdcp.test002;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.tplink.applibs.util.TPByteArrayJNI;
import com.tplink.sdk.tpopensdk.TPOpenSDK;
import com.tplink.sdk.tpopensdk.TPPlayer;
import com.tplink.sdk.tpopensdk.TPSDKContext;
import com.tplink.sdk.tpopensdk.common.TPSDKCommon;
import com.tplink.sdk.tpopensdk.openctx.IPCDevice;
import com.tplink.sdk.tpopensdk.openctx.IPCDeviceContext;
import com.tplink.sdk.tpopensdk.openctx.IPCReqListener;
import com.tplink.sdk.tpopensdk.openctx.IPCReqResponse;

import java.nio.ByteBuffer;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    private Button connBtn;
    private FrameLayout playFrameLayout;
    private Button upBtn, downBtn, leftBtn, rightBtn;
    private static final String IPC_IP = "172.20.85.13";
    private static final int IPC_PORT = 80;
    private static final String IPC_PWD = "123456";

    public void initView() {
        connBtn = findViewById(R.id.conn_main_btn);
        upBtn = findViewById(R.id.up_main_btn);
        downBtn = findViewById(R.id.down_main_btn);
        leftBtn = findViewById(R.id.left_main_btn);
        rightBtn = findViewById(R.id.right_main_btn);
        playFrameLayout = findViewById(R.id.player_view_holder);
        connBtn.setOnClickListener(this);
        upBtn.setOnClickListener(this);
        downBtn.setOnClickListener(this);
        leftBtn.setOnClickListener(this);
        rightBtn.setOnClickListener(this);
    }


    private TPSDKContext mSdkContext;
    private IPCDeviceContext devCtx;
    private IPCDevice dev;
    private TPPlayer mPlayer;
    private int xCoord = 0, yCoord = 0;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.conn_main_btn: // 初始化摄像机
                this.initIPCConn();
                break;
            case R.id.up_main_btn: // 向上转动
                yCoord += 10;
                move();
                break;
            case R.id.down_main_btn: // 向下转动
                yCoord += -10;
                move();
                break;
            case R.id.left_main_btn: // 向左转动
                xCoord += -10;
                move();
                break;
            case R.id.right_main_btn: // 向右转动
                xCoord += 10;
                move();
                break;

        }

    }

    public void initIPCConn() {
        mSdkContext = TPOpenSDK.getInstance().getSDKContext();
        mSdkContext.appReqStart(true, null);
        devCtx = mSdkContext.getDevCtx();
        dev = new IPCDevice(devCtx.initDev(IPC_IP, IPC_PORT));
        devCtx.reqLogin(dev, IPC_PWD, new IPCReqListener() {
            @Override
            public int callBack(IPCReqResponse ipcReqResponse) {
                String loginResult = "";
                // 登录成功的判断
                if (ipcReqResponse.mError == 0) {
                    loginResult = "登录设备成功...";
                    // App请求链接摄像机设备
                    devCtx.reqConnectDev(dev, new IPCReqListener() {
                        @Override
                        public int callBack(IPCReqResponse ipcReqResponse) {
                            String connResult = "";
                            if (ipcReqResponse.mError == 0) {
                                connResult = "连接设备成功...";
                                MainActivity.this.initPlayer();
                            } else {
                                connResult = "连接设备失败...";
                            }
                            final String finalConnResult = connResult;
                            MainActivity.this.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MainActivity.this, finalConnResult, Toast.LENGTH_SHORT).show();
                                }
                            });
                            return 0;
                        }
                    });
                } else {
                    loginResult = "登录设备失败...";
                }
                final String finalLoginResult = loginResult;
                MainActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this, finalLoginResult, Toast.LENGTH_SHORT).show();
                    }
                });
                return 0;
            }
        });
    }

    public void initPlayer() {
        devCtx.reqGetVideoPort(dev, new IPCReqListener() {
            @Override
            public int callBack(IPCReqResponse ipcReqResponse) {
                if (ipcReqResponse.mError == 0) {
                    mPlayer = TPOpenSDK.getInstance()
                            .createPlayer(MainActivity.this)
                            .setViewHolder(playFrameLayout);
                    mPlayer.setDecodeMode(TPSDKCommon.PlayerDecodeMode.TPPLAYER_DECODE_MODE_SOFTWARE_ONLY);
                    mPlayer.setPlayerCallback(new TPPlayer.PlayerCallback() {
                        @Override
                        public int onPlayStatusChange(int status, int errorCode) {
                            System.out.println("错误码：" + errorCode);
                            return 0;
                        }
                        @Override
                        public int onRecordStatusChange(int status, int errorCode, String filePath) {
                            return 0;
                        }
                        @Override
                        public int onSnapshot(int errorCode, String filePath) {
                            return 0;
                        }
                        @Override
                        public int onPlayTimeUpdate(final long playTime) {
                            return 0;
                        }

                        @Override
                        public int onRecordDurationUpdate(final long duration) {
                            return 0;
                        }

                        @Override
                        public int onDataStatistics(final long dataSize, final double dataSpeed) {
                            return 0;
                        }

                        @Override
                        public int onChangeQuality(int quality) {
                            return 0;
                        }

                        @Override
                        public int onDataRecv(TPByteArrayJNI tpByteArrayJNI) {
                            return 0;
                        }

                        @Override
                        public int onYUVDataRecv(ByteBuffer[] byteBuffers, int[] ints, int i, int i1) {
                            return 0;
                        }
                    });
                    mPlayer.startRealPlay(dev, TPSDKCommon.Quality.QUALITY_FLUENCY);
                }
                return 0;
            }
        });
    }
    public void move() {
        devCtx.reqMotorMoveTo(dev, new IPCReqListener() {
            @Override
            public int callBack(IPCReqResponse ipcReqResponse) {
                System.out.println(ipcReqResponse.mError);
                return 0;
            }
        }, xCoord, yCoord, -1);
}}